Skip to content

Fix blockscaled GEMV K bounds and scalar tails - #3567

Open
TANGBUDU wants to merge 3 commits into
NVIDIA:mainfrom
TANGBUDU:fix/blockscaled-gemv-k-tail
Open

TANGBUDU wants to merge 3 commits into
NVIDIA:mainfrom
TANGBUDU:fix/blockscaled-gemv-k-tail

Conversation

@TANGBUDU

@TANGBUDU TANGBUDU commented Aug 29, 2026 •

Copy link
Copy Markdown

Fixes #3536, fixes #3568.

The mainloop always consumes whole four-stage buffers. When the number of complete K tiles is not a multiple of kStageCount, the extra stages either read past the end of K (into the next row or batch, or out of bounds) or reuse stale shared memory, and the scalar tail starts from the prefetch cursor instead of the end of the accumulated range. For example, --m=256 --k=1280 --batch=2 fails verification and memcheck reports out-of-bounds global reads.

Fix:

  • Predicate each stage load against the end of the complete tiles and zero-fill invalid stages with cp_async_zfill, so they add nothing in the FMA.
  • Start the scalar tail at mainloop_k.

Writer ownership and synchronization are unchanged. Thanks to @VaggelisGian for the original report.

Tests: kept the 8 tail cases, added 6 stage-boundary cases with batch > 1, and added --test-k-boundaries, which checks isolated last-tile/tail contributions and that changing batch 1 inputs does not change batch 0. All 15 pass on sm_120a with CUDA 13.0, and memcheck, racecheck and synccheck are clean on the boundary cases. In a 152-shape sweep, 149 pass, up from 54 on main, with no new failures. The remaining 3 (K=32/64, batch 2/8) go through the tail-only path, fail the same way on main, and are not touched here.

Perf: register usage goes from 80 to 92, and aligned shapes pay a small cost. Paired CUDA Graph timings against the previous head on an RTX 5070 Ti Laptop (noisy, so treat as rough):

(M, K, batch) Change
(256, 1024, 1) +1.3%
(256, 4096, 3) +4.4%
(1024, 4096, 1) +9.9%
(1024, 8192, 3) +2.9%
(4096, 4096, 1) +6.1%

If that matters, a variant that keeps the old unpredicated loads when all four stages are valid gets back to 80 registers with identical results. Happy to switch to it.

The tail guard used unroll_col_k, which is the gmem prefetch issue cursor. The
prologue primes one buffer and each mainloop iteration issues one more ahead of
consumption, so at loop exit unroll_col_k leads the accumulated K range by
kStageCount * tileA_k_local.

When floor(gemm_k / tileA_k_local) is a multiple of kStageCount and gemm_k is
not tile aligned, the guard is false for every thread, process_tail_elements()
is skipped, and up to a tile of K is dropped from the dot product.

tile_idx advances by kStageCount per iteration and the FMA sequence within an
iteration is unconditional, so tile_idx * tileA_k_local at loop exit is the K
range that was accumulated. Use it as the tail origin. ptr_A and ptr_B are not
advanced by the mainloop, so the absolute-K addressing inside
process_tail_elements() is unaffected.

ctest cases on example 91: k=1056, 1152 and 2176 fail before this change and
pass after. k=288, 1280 and 1312 are controls against using
total_tiles * tileA_k_local as the origin, which would double count.

Addresses NVIDIA#3536, reported by @VaggelisGian.

Not covered here: the same issue notes loads issued past the current K range.
Since the FMA sequence is unconditional, the mainloop always runs a whole
multiple of kStageCount tiles and accumulates past gemm_k when
floor(gemm_k / tileA_k_local) % kStageCount != 0. Visible as wrong results for
batch > 1, e.g. --m=256 --k=1280 --batch=2, and needs per-stage load/FMA
predication.
@TANGBUDU

Copy link
Copy Markdown
Author

@Junkai-Wu Hi, could you take a look at this PR, or point me to the right reviewer? It fixes #3536: blockscaled GEMV drops part of the K tail when K is not tile-aligned and the tile count is a multiple of kStageCount, so the result is wrong. The PR adds ctest cases on example 91; the K=1056, 1152 and 2176 cases fail before the fix and pass after. Thanks!

@TANGBUDU
TANGBUDU marked this pull request as draft September 26, 2026 16:53
@TANGBUDU TANGBUDU changed the title Fix dropped K tail in blockscaled GEMV Fix blockscaled GEMV K bounds and scalar tails Sep 26, 2026
@TANGBUDU
TANGBUDU marked this pull request as ready for review September 26, 2026 17:38

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant